/**
 * 
 */

package com.xxxx.game;

import com.example.gametestproject.SpriteBasic;

import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

/**
 * @author HuXiaolong
 *
 */
public class SpriteSelectionAnimation extends AnimationBasic implements ICanvasDrawing, IPaintAnimation
{
	private int mSelectionTime = 0;
	float angle = 0;
	boolean decrease = false;
	private ColorMatrix cm = new ColorMatrix();
	public SpriteSelectionAnimation()
	{
	}

	@Override
	public boolean paint(Canvas canvas, ISprite sprite)
	{
		float centerX = sprite.getPosition().x + sprite.getSize().Width / 2;
		float centerY = sprite.getPosition().y + sprite.getSize().Height / 2;
		float scaleRatio = 1.0f / 20;
		if (mSelectionTime <= 4)
		{
			float xscale = 1.0f, yscale = 1.0f;
			xscale += scaleRatio * mSelectionTime;
			yscale -= scaleRatio * mSelectionTime;
			mSelectionTime ++;
			canvas.scale(xscale, yscale, centerX, centerY);
		}
		
		if (mSelectionTime > 4 && mSelectionTime <= 8)
		{
			float xscale = 1.2f, yscale = 0.8f;
			xscale -= scaleRatio * mSelectionTime;
			yscale += scaleRatio * mSelectionTime;
			canvas.scale(xscale, yscale, centerX, centerY);
		}
		
		return mSelectionTime < 8;
	}

	@Override
	public boolean update(ISprite sprite)
	{
		if (this.mSelectionTime >= 8)
		{
			if(((SpriteBasic)sprite).isSelected())
				this.update(sprite.getPaint());
			else
				sprite.getPaint().setColorFilter(null);
			return ((SpriteBasic)sprite).isSelected();
		}
		++ this.mSelectionTime;
		return true;
	}

	@Override
	public boolean isMoving()
	{
		return false;
	}

	@Override
	public String getName()
	{
		// TODO Auto-generated method stub
		return "SelectionAnimation";
	}

	@Override
	public Paint update(Paint paint) {
		if(angle == 120)
			this.decrease = true;
		else if(angle == 0)
			this.decrease = false;
		if(decrease)
			angle -= 4;
		else 
			angle += 4;
		
		float contrast = angle / 180.f;
		
		this.setContrastTranslateOnly(cm, contrast);
		paint.setColorFilter(new ColorMatrixColorFilter(cm));
		return null;
	}
	
	private void setContrastScaleOnly(ColorMatrix cm, float contrast) {
	    float scale = contrast + 1.f;
	       float translate = (-.5f * scale + .5f) * 255.f;
	    cm.set(new float[] {
	           scale, 0, 0, 0, 0,
	           0, scale, 0, 0, 0,
	           0, 0, scale, 0, 0,
	           0, 0, 0, 1, 0 });
	}
	private void setContrast(ColorMatrix cm, float contrast) {
	    float scale = contrast + 1.f;
	       float translate = (-.5f * scale + .5f) * 255.f;
	    cm.set(new float[] {
	           scale, 0, 0, 0, translate,
	           0, scale, 0, 0, translate,
	           0, 0, scale, 0, translate,
	           0, 0, 0, 1, 0 });
	}

	private void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
	    float scale = contrast + 1.f;
	       float translate = (-.5f * scale + .5f) * 255.f;
	    cm.set(new float[] {
	           1, 0, 0, 0, translate,
	           0, 1, 0, 0, translate,
	           0, 0, 1, 0, translate,
	           0, 0, 0, 1, 0 });
	}
}
